package com.dongliu.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dongliu.reggie.common.BaseContext;
import com.dongliu.reggie.common.R;
import com.dongliu.reggie.dto.DishDto;
import com.dongliu.reggie.entity.Category;
import com.dongliu.reggie.entity.Dish;
import com.dongliu.reggie.entity.Orders;
import com.dongliu.reggie.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 订单
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    /**
     * 用户提交订单
     * @param order
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders order){
        log.info("订单数据：{}", order);
        orderService.submit(order);
        return R.success("下单成功");
    }

    /**
     * 菜品信息分页查询
     * @param page
     * @param pageSize
     * @param number
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page, int pageSize, Long number, String beginTime, String endTime){
        // 构造分页构造器对象
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        // 构造条件构造器对象
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        // 添加过滤条件
        queryWrapper.like(number != null, Orders::getNumber, number);
        //1.具有转换功能的对象
        if(beginTime != null && endTime != null){
            DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            LocalDateTime time1 = LocalDateTime.parse(beginTime, df);
            LocalDateTime time2 = LocalDateTime.parse(endTime, df);
            queryWrapper.between(Orders::getOrderTime, time1, time2);
            // 添加排序条件
            queryWrapper.orderByDesc(Orders::getOrderTime);
        }

        // 执行分页查询
        orderService.page(pageInfo, queryWrapper);

        return R.success(pageInfo);
    }

    /**
     * 用户获取订单信息
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("userPage")
    public R<Page> getUserPage(int page, int pageSize){
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        Long userId = BaseContext.getCurrentId();
        queryWrapper.eq(Orders::getUserId, userId);
        orderService.page(pageInfo, queryWrapper);
        return R.success(pageInfo);
    }

    /**
     * 订单状态更新
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> setStatus(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("订单状态更新成功");
    }

}
